<?php

namespace app\dao;

abstract class BaseDao
{
    abstract protected function setModel();

    protected function getModel(){
        return app()->make($this->setModel());
    }

    public function find($id){
        return $this->getModel()->find($id);
    }

    public function create($data){
        $model = $this->getModel()->create($data);
        return $this->getModel()->find($model->id);
    }

    public function exists($id)
    {
        return $this->getModel()->where('id', '=', $id)->count() > 0;
    }

    public function existsIn($ids)
    {
        return $this->getModel()->whereIn('id', $ids)->count() == count($ids);
    }

    public function getIn($ids)
    {
        return $this->getModel()->whereIn('id', $ids)->select();
    }

    public function delete($id){
        $this->getModel()->destroy($id);
        return "ok";
    }

    public function edit($id, $data){
        $this->getModel()->where('id', $id)->update($data);
        return $this->getModel()->find($id);
    }

    public function updateById($filed,$value,$id){
        $this->getModel()->where('id' , $id)->update([$filed => $value]);
        return 'ok';
    }
}